/**
 * FileName: RoleServiceImpl.java
 * 角色业务实现类
 *
 * @author  王威
 * @Date    2021/12/1
 * @version 1.00
 **/
package com.site.service.impl;

import com.site.dao.RoleDao;
import com.site.dao.impl.RoleDaoImpl;
import com.site.pojo.Role;
import com.site.service.RoleService;
import com.site.utils.JDBCUtils;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class RoleServiceImpl implements RoleService {
    private RoleDao roleDao = new RoleDaoImpl();
    @Override
    public List<Role> selectAll() throws SQLException {
        return roleDao.selectAll();
    }

    @Override
    public int getEmployeeRoleRid(Role role) throws SQLException {
        int num = 0;
        Connection con = JDBCUtils.getConnection();
        String sql = "SELECT r.`rid` FROM employee AS e,`employee_role` AS er,`role_access` AS ra,ROLE AS r\n" +
                "WHERE e.eid = er.`eid` AND er.`rid` = r.`rid` AND r.`role_name`= ? GROUP BY r.`rid`";
        ResultSet set = JDBCUtils.executeQuery(con,sql,new Object[]{role.getRoleName()});
        while (set.next()){
            num = set.getInt(1);
        }
        return num;
    }

    @Test
    void test(){
        Role role = new Role();
        role.setRoleName("系统管理员");
        try {
            System.out.println(getEmployeeRoleRid(role));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
